[p4python] Add partial annotations for P4 and P4API modules #15340
+818
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add annotations for
P4andP4APImodules fromp4python(https://pypi.org/project/p4python/) Annotations forP4.P4class is very incomplete.Having now done the work to extract typing information from our internal
p4pythonwrapper, I've become less convinced in the value of attempting to provide comprehensive annotation for this library. Accurately annotatingP4.P4actually removes a lot of the utility of having these types in the first place: the implementation is simply too flexible in how it operates, owning to maintaining so many years of backwards compatibility and less-than-ideal architectural decisions. The wrapper I extracted this from is much more opinionated, limited in scope, and removes a lot of flexibility to for the sake of maintainability.I see three ways forward with this PR, which I'd like to get some feedback on:
1. As-is
Accept roughly in its current shape, with the understanding that
P4.P4will likely never be more accurately annotated. It will potentially not receive much community attention in terms of continued investment, despite it having tens of thousands of downloads a day.Speaking personally, I would much rather put effort towards polishing and open sourcing an opinionated wrapper that I would be happy to put my and my employer's name on.
2. Abandon attempting to annotate
P4.run_*(),P4.fetch_*(),P4.save_*()et alThe main pain point is that
P4.P4uses__getattr__()to materialize all supported commands as methods. The effective surface area is huge, but not all known commands can operate in every mode. Each command also require or accept different parameters.However, there is very little churn in the API from release-to-release. The foundational classes implemented in C++ (
P4API.P4Adapter,P4API.P4Mapet al) also see very little change, if at all. There are still useful annotations outside ofP4.P4that will be critical for anyone attempting to wrapp4pythonwith a better API for their own use. These classes are annotated comprehensively, and, I feel, have value.For now, I think this is my preferred way forward.
3. Abandon for now
Given that it's proven not feasible for us to significantly down-scope our internal wrapper library in favour of good type annotation on
p4pythonitself, I'm content to chalk this up to a useful learning experience and move on.